using System;
using System.Collections.Generic;
using System.Linq;
namespace TemplateWeb.Controllers
{
	#region using
	using System.Web.Mvc;
	using TemplateWeb.Models.ViewModels;
	using TemplateWeb.Models;
	using TemplateWeb.Models.Repositories;
	using TemplateWeb.Helpers;
	using System.Web.Security;
	#endregion
	public partial class AdminController: BaseController
	{
		public ActionResult SelectAlbum()
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_ALBUM
			};
			return View(CreateViewModel(data));
		}

		public ActionResult SelectAlbumPaging(int page = 1)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_ALBUM,
				CurrentPage = page
			};
			return PartialView("Templates/TH_ListAlbum", CreateViewModel(data));
		}

		public ActionResult SelectAlbumByIDLoaiVideo(int idloaivideo, int page = 1)
		{
			var data = new DataTransferViewModel
			{
				IDLoaiVideo = idloaivideo,
				EnumViewModelType = EnumViewModel.ADMIN_ALBUM,
				CurrentPage = page
			};
			return View("SelectAlbum", CreateViewModel(data));
		}

		public ActionResult SelectAlbumByIDLoaiVideoPaging(int idloaivideo, int page = 1)
		{
			var data = new DataTransferViewModel
			{
				IDLoaiVideo = idloaivideo,
				EnumViewModelType = EnumViewModel.ADMIN_ALBUM,
				CurrentPage = page
			};
			return PartialView("Templates/TH_ListAlbum", CreateViewModel(data));
		}

        public ActionResult DeleteAlbum(int id, int page = 1)
        {
            var deleteItem = _repAlbum.SelectByID(id);
            var fileName = deleteItem.HinhMinhHoa;
            var idloaivideo = deleteItem.IDLoaiVideo;
            var result = _repAlbum.Delete(id);
            if (result)
            {
                if (fileName != "noimages.jpg")
                {
                    var fullPath = Server.MapPath("/Content/Images/Items/" + fileName);
                    FileHelper.DeleteFile(fullPath);
                }
            }
            var data = new DataTransferViewModel
            {
                InfoText = result ? "Item has been deleted" : "Cannot delete item!",
                EnumViewModelType = EnumViewModel.ADMIN_ALBUM,
                CurrentPage = page,
                IDLoaiVideo = idloaivideo,
            };
            return View("SelectAlbum", CreateViewModel(data));
        }

		public ActionResult DetailOfAlbum(int id)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_DETAILOF_ALBUM,
				IntID = id
			};
			return View(CreateViewModel(data));
		}

		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult UpdateAlbum(FormCollection forms)
		{
			string id = forms.GetValues("Album_ID")[0];
			string idloaivideo = forms.GetValues("Album_IDLoaiVideo")[0];
			string tenalbum = forms.GetValues("Album_TenAlbum")[0];
			string gioithieu = forms.GetValues("Album_GioiThieu")[0];
			var file = Request.Files["Album_HinhMinhHoa"];
			string refName = "";
			var editItem = _repAlbum.SelectByID(int.Parse(id));
			editItem.IDLoaiVideo = int.Parse(idloaivideo.Replace(".", "").Replace(",", ""));
			editItem.TenAlbum = tenalbum;
			editItem.GioiThieu = gioithieu;
			var result = false;
			if (file.ContentLength != 0)
			{
                result = FileHelper.UploadFile(file, Server.MapPath("/Content/Images/Items/"), id, editItem.HinhMinhHoa, WebConfiguration.SmallImageWidth, WebConfiguration.SmallImageHeight, ref refName);
				editItem.HinhMinhHoa = refName;
			}
			result = _repAlbum.Save();
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_DETAILOF_ALBUM,
				IntID = int.Parse(id.Replace(".", "").Replace(",", "")),
				InfoText = result ? "Item has been updated" : "Cannot update this item"
			};
			return View("DetailOfAlbum", CreateViewModel(data));
		}


		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult InsertAlbum(FormCollection forms)
		{
			string idloaivideo = forms.GetValues("Album_IDLoaiVideo")[0];
			string tenalbum = forms.GetValues("Album_TenAlbum")[0];
			string gioithieu = forms.GetValues("Album_GioiThieu")[0];
			string tennguoidang = forms.GetValues("Album_TenNguoiDang")[0];
			var file = Request.Files["Album_HinhMinhHoa"];
			string refName = "";
			var newItem = new Album
			{
				IDLoaiVideo = int.Parse(idloaivideo),
				TenAlbum = tenalbum,
				GioiThieu = gioithieu,
				NgayDang = DateTime.Now,
				TenNguoiDang = tennguoidang,
				HinhMinhHoa = "noimages.jpg"
			};
			var result = _repAlbum.Insert(newItem);
			if (result)
			{
				if (file.ContentLength != 0)
				{
                    result = FileHelper.UploadFile(file, Server.MapPath("/Content/Images/Items/"), newItem.ID.ToString(), string.Empty, WebConfiguration.SmallImageWidth, WebConfiguration.SmallImageHeight, ref refName);
					if (result)
					{
						var addedItem = _repAlbum.SelectByID(newItem.ID);
						addedItem.HinhMinhHoa = refName;
						_repAlbum.Save();
					}
				}
			}
			var data = new DataTransferViewModel
			{
				IDLoaiVideo = int.Parse(idloaivideo),
				EnumViewModelType = EnumViewModel.ADMIN_ALBUM,
				InfoText = result ? "New item has been added" : "Cannot insert new item"
			};
			return View("SelectAlbum", CreateViewModel(data));
		}


	}
}